#include "QQIconButton.h"

Q_PROPERTY_CREATE_Q_CPP(QQIconButton, int, BorderRadius)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, qreal, Opacity)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, NormalColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, HoverColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, PressColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, SelectedColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, NormalIconColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, HoverIconColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, PressIconColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, QColor, SelectedIconColor)
Q_PROPERTY_CREATE_Q_CPP(QQIconButton, bool, IsSelected)

QQIconButton::QQIconButton(QPixmap pix, QWidget* parent)
	:QPushButton(parent), d_ptr(new QQIconButtonPrivate)
{
	Q_D(QQIconButton);
	d->q_ptr = this;
	d->_iconPix = pix.copy();
	d->_pOpacity = 1;
	d->_pNormalColor = Qt::transparent;
	d->_pHoverColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pPressColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pSelectedColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pNormalIconColor = Qt::black;
	d->_pHoverIconColor = Qt::black;
	d->_pPressIconColor = Qt::black;
	d->_pSelectedIconColor = Qt::black;
	d->_pIsSelected = false;
	d->_pBorderRadius = 0;
	connect(this, &QQIconButton::pIsSelectedChanged, this, [=]() { update(); });
}

QQIconButton::QQIconButton(ElaIconType::IconName awesome, QWidget* parent)
	:QPushButton(parent), d_ptr(new QQIconButtonPrivate)
{
	Q_D(QQIconButton);
	d->q_ptr = this;
	d->_pOpacity = 1;
	d->_pNormalColor = Qt::transparent;
	d->_pHoverColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pPressColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pSelectedColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pNormalIconColor = Qt::black;
	d->_pHoverIconColor = Qt::black;
	d->_pPressIconColor = Qt::black;
	d->_pSelectedIconColor = Qt::black;
	d->_pIsSelected = false;
	d->_pBorderRadius = 0;
	QFont iconFont = QFont("ElaAwesome");
	iconFont.setPixelSize(15);
	this->setFont(iconFont);
	d->_pAwesome = awesome;
	this->setText(QChar((unsigned short)awesome));
	connect(this, &QQIconButton::pIsSelectedChanged, this, [=]() { update(); });
}

QQIconButton::QQIconButton(ElaIconType::IconName awesome, int pixelSize, QWidget* parent)
	:QPushButton(parent), d_ptr(new QQIconButtonPrivate)
{
	Q_D(QQIconButton);
	d->q_ptr = this;
	d->_pOpacity = 1;
	d->_pNormalColor = Qt::transparent;
	d->_pHoverColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pPressColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pSelectedColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pNormalIconColor = Qt::black;
	d->_pHoverIconColor = Qt::black;
	d->_pPressIconColor = Qt::black;
	d->_pSelectedIconColor = Qt::black;
	d->_pIsSelected = false;
	d->_pBorderRadius = 0;
	QFont iconFont = QFont("ElaAwesome");
	iconFont.setPixelSize(pixelSize);
	this->setFont(iconFont);
	d->_pAwesome = awesome;
	this->setText(QChar((unsigned short)awesome));
	connect(this, &QQIconButton::pIsSelectedChanged, this, [=]() { update(); });
}

QQIconButton::QQIconButton(ElaIconType::IconName awesome, int pixelSize, int fixedWidth, int fixedHeight, QWidget* parent)
	:QPushButton(parent), d_ptr(new QQIconButtonPrivate)
{
	Q_D(QQIconButton);
	d->q_ptr = this;
	d->_pOpacity = 1;
	d->_pNormalColor = Qt::transparent;
	d->_pHoverColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pPressColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pSelectedColor = QColor(0xCC, 0xCC, 0xCC, 60);
	d->_pNormalIconColor = Qt::black;
	d->_pHoverIconColor = Qt::black;
	d->_pPressIconColor = Qt::black;
	d->_pSelectedIconColor = Qt::black;
	d->_pIsSelected = false;
	d->_pBorderRadius = 0;
	QFont iconFont = QFont("ElaAwesome");
	iconFont.setPixelSize(pixelSize);
	this->setFont(iconFont);
	d->_pAwesome = awesome;
	this->setText(QChar((unsigned short)awesome));
	this->setFixedSize(fixedWidth, fixedHeight);
	connect(this, &QQIconButton::pIsSelectedChanged, this, [=]() { update(); });
}

QQIconButton::~QQIconButton()
{
}

void QQIconButton::setAwesome(ElaIconType::IconName awesome)
{
	Q_D(QQIconButton);
	d->_pAwesome = awesome;
	this->setText(QChar((unsigned short)awesome));
}

ElaIconType::IconName QQIconButton::getAwesome() const
{
	return this->d_ptr->_pAwesome;
}

void QQIconButton::setPixmap(QPixmap pix)
{
	Q_D(QQIconButton);
	d->_iconPix = pix.copy();
}

bool QQIconButton::event(QEvent* event)
{
	Q_D(QQIconButton);
	switch (event->type())
	{
	case QEvent::MouseButtonPress:
	{
		if (isEnabled() && !d->_pIsSelected)
		{
			d->_pIsPressed = true;
		}
		break;
	}
	case QEvent::MouseButtonRelease:
	{
		if (isEnabled() && !d->_pIsSelected)
		{
			d->_pIsPressed = false;
		}
		break;
	}
	default:
	{
		break;
	}
	}
	return QPushButton::event(event);
}

void QQIconButton::paintEvent(QPaintEvent* event)
{
	Q_D(QQIconButton);
	QPainter painter(this);
	painter.save();
	painter.setOpacity(d->_pOpacity);
	painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing);
	painter.setPen(Qt::NoPen);

	painter.setBrush(
		d->_pIsSelected ? getSelectedColor() :
		isEnabled() ?
		d->_pIsPressed ? getPressColor() :
		underMouse() ? getHoverColor() : getNormalColor()
		: Qt::transparent
	);
	painter.drawRoundedRect(rect(), d->_pBorderRadius, d->_pBorderRadius);

	if (!d->_iconPix.isNull())
	{
		QPainterPath path;
		path.addEllipse(rect());
		painter.setClipPath(path);
		painter.drawPixmap(rect(), d->_iconPix);
	}
	else
	{
		painter.setPen(
			d->_pIsSelected ? getSelectedIconColor() :
			isEnabled() ?
			d->_pIsPressed ? getPressIconColor() :
			underMouse() ? getHoverIconColor() : getNormalIconColor()
			: Qt::gray
		);
		painter.drawText(rect(), Qt::AlignCenter, QChar((unsigned short)d->_pAwesome));
	}
	painter.restore();
}